Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Collections.Generic

' way
Imports Way.Utils.Xml

Public Class UserConfig

    Shared carregado As Boolean = False
    Shared user As String = "user"
    Shared doc As XmlDocument

    Shared ConfigMenu As XmlNode
    Shared ConfigTheme As XmlNode
    Shared ConfigScreen As XmlNode

    Shared APPDATA_DIR_CONFIG As String
    Shared APPDATA_CONFIG As String

    Public Const CONFIG_SCREEN_SHORTCUTOPEN As String = "shortcutopen"
    Public Const CONFIG_SCREEN_SHORTCUTINDEX As String = "shortcutindex"
    Public Const CONFIG_SCREEN_RIBBONOPEN As String = "ribbonopen"

    Public Const CONFIG_MENU As String = "/configuration/config.menu/config.tabs"
    Public Const CONFIG_SCREEN As String = "/configuration/config.screen"
    Public Const CONFIG_THEME As String = "/configuration/config.theme"

    Public Const CONFIG_MENU_MOVIMENTACAO As String = "/configuration/config.menu/config.tabs/movimentacao/items"
    Public Const CONFIG_MENU_CADASTRO As String = "/configuration/config.menu/config.tabs/cadastro/items"
    Public Const CONFIG_MENU_LOGISTICA As String = "/configuration/config.menu/config.tabs/logistica/items"
    Public Const CONFIG_MENU_PDV As String = "/configuration/config.menu/config.tabs/pdv/items"
    Public Const CONFIG_MENU_RELOPER As String = "/configuration/config.menu/config.tabs/relatorio/operacional/items"
    Public Const CONFIG_MENU_RELGER As String = "/configuration/config.menu/config.tabs/relatorio/gerencial/items"

    Public Shared Sub CarregarConfig(ByVal _user As String)
        carregado = False
        Try
            If (Not String.IsNullOrEmpty(_user)) Then user = _user
            APPDATA_DIR_CONFIG = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() _
                & "\Softway\Gestor.NET\"
            APPDATA_CONFIG = APPDATA_DIR_CONFIG & user & ".config"

            CheckFolder()

            doc = New XmlDocument()
            doc.PreserveWhitespace = False
            If (File.Exists(APPDATA_CONFIG)) Then
                doc.LoadXml(File.ReadAllText(APPDATA_CONFIG))
            End If
            carregado = True

            ConfigMenu = doc.SelectSingleNode(CONFIG_MENU)
            ConfigTheme = doc.SelectSingleNode(CONFIG_THEME)
            ConfigScreen = doc.SelectSingleNode(CONFIG_SCREEN)
        Catch ex As Exception
        End Try
    End Sub

    Shared Sub CheckFolder()
        If (Not Directory.Exists(APPDATA_DIR_CONFIG)) Then
            Directory.CreateDirectory(APPDATA_DIR_CONFIG)
        End If
    End Sub
    Public Shared Function GetCommand(ByVal comm As String) As String
        If (String.IsNullOrEmpty(comm)) Then Return String.Empty
        If (comm.Split("|"c).Length > 4) Then
            Return String.Format("{0}|{1}", comm.Split("|"c)(3), comm.Split("|"c)(4))
        End If
        Return comm
    End Function

    Public Shared Function GetNameFull(ByVal comm As String) As String
        If (String.IsNullOrEmpty(comm)) Then Return String.Empty
        If (comm.Split("|"c).Length > 4) Then
            Dim groupName As String = comm.Split("|"c)(1)
            Return String.Format("{0}{1}", IIf(String.IsNullOrEmpty(groupName), "", groupName & " - "), comm.Split("|"c)(2))
        End If
        Return comm
    End Function

    Public Shared Function GetChecked(ByVal comm As String) As Boolean
        If (String.IsNullOrEmpty(comm)) Then Return False
        If (comm.Split("|"c).Length > 4) Then
            Return (comm.Split("|"c)(5) = "1")
        End If
        Return False
    End Function

    Public Shared Function GetConfigMenu(ByVal path As String) As String()
        If (Not carregado) Then CarregarConfig(user)
        Dim arr As New List(Of String)
        For Each node As XmlNode In ConfigMenu.SelectSingleNode(path).ChildNodes
            arr.Add(String.Format("{0}|{1}|{2}|{3}|{4}|{5}" _
                , XmlUtil.GetAttValue(node, "id") _
                , XmlUtil.GetAttValue(node, "groupname") _
                , XmlUtil.GetAttValue(node, "name") _
                , XmlUtil.GetAttValue(node, "commandname") _
                , XmlUtil.GetAttValue(node, "commandsource") _
                , XmlUtil.GetAttValue(node, "checked")))
        Next
        Return arr.ToArray()
    End Function

    Public Shared Function GetConfigTheme() As String
        If (Not carregado) Then CarregarConfig(user)
        Return XmlUtil.GetAttValue(ConfigTheme, "palletemode")
    End Function

    Public Shared Function GetConfigScreen() As String()
        If (Not carregado) Then CarregarConfig(user)

        Dim arr As New List(Of String)
        For Each node As XmlNode In ConfigScreen.ChildNodes
            arr.Add(String.Format("{0}|{1}", XmlUtil.GetAttValue(node, "name"), XmlUtil.GetAttValue(node, "value")))
        Next
        Return arr.ToArray()
    End Function
    Public Shared Sub SetConfigTheme(ByVal config As String)
        If (Not carregado) Then CarregarConfig(user)
        If (ConfigTheme.Attributes("palletemode") IsNot Nothing) Then
            ConfigTheme.Attributes("palletemode").Value = config
        End If
    End Sub

    Public Shared Sub SetConfigScreen(ByVal items() As String)
        If (Not carregado) Then CarregarConfig(user)
        For Each item As String In items
            Dim node As XmlNode = doc.SelectSingleNode(String.Concat(CONFIG_SCREEN, "/screen.item[@name='", item.Split("|"c)(0), "']"))
            If (node IsNot Nothing AndAlso node.Attributes("value") IsNot Nothing) Then
                node.Attributes("value").Value = item.Split("|"c)(1)
            End If
        Next
    End Sub

    Public Shared Sub SalvarConfig()
        If (Not carregado) Then CarregarConfig(user)
        doc.Save(APPDATA_CONFIG)
        doc = Nothing
        carregado = False
    End Sub

    Public Shared Sub ResetarConfig()
        Dim docReset As New XmlDocument()
        docReset.Save(APPDATA_CONFIG)
        CarregarConfig(user)
    End Sub

End Class
